package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.SearchHistoryMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 杨杰
 * @version V1.0
 * @Package com.cskaoyan.service
 * @date 2020/12/5 20:36
 * @Copyright © 暂时还没有
 */
@Service
public class AdminHistoryServiceImp implements AdminHistoryService{
    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Override
    public ListData<SearchHistory> adminHistoryList
            (Integer page, Integer limit, String sort, String order, Integer userId, String keyword) {

        //分页
        PageHelper.startPage(page,limit);

        //设置sql语句
        SearchHistoryExample historyExample = new SearchHistoryExample();
        historyExample.setOrderByClause(sort+" " + order);
        SearchHistoryExample.Criteria criteria = historyExample.createCriteria();
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if (keyword != null){
            criteria.andKeywordLike("%" + keyword + "%");
        }

        //查询
        ListData<SearchHistory> listData = new ListData<>();
        List<SearchHistory> histories= searchHistoryMapper.selectByExample(historyExample);
        //封装listData的items，total属性
        listData.setItems(histories);
        PageInfo pageInfo = new PageInfo(histories);
        long total = pageInfo.getTotal();
        listData.setTotal((int)total);

        return listData;
    }
}
